/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.cancion;


import entagged.audioformats.AudioFile;
import entagged.audioformats.AudioFileIO;
import entagged.audioformats.exceptions.CannotReadException;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author triki
 */

/*
 * Prototipo de ruta local:
 * - /[path_to_folder]/
 *
 * Prototipo de ruta remota:
 * - [userID]/
 

 */

public class Cancion {
    private String userID;
    private String nombre;
    private String path;

    private String nombre_cancion;
    private String nombre_artista;
    private String nombre_disco;
    private String duracion;
    
    private boolean local;

    public Cancion(String nom,String path,boolean local)
    {
        nombre = new String(nom);
        this.path = new String(path);
        this.local = local;
        loadMetadata();
    }

    public boolean is_local()
    {
        if(local)
            return true;
        return false;
    }

    public String getFilename()
    {
        return nombre;
    }

    public String getRealPath()
    {
        return path+nombre;
    }

    public String getPath()
    {
        return path;
    }

    public String getDuracion()
    {
        return duracion;
    }

    private void loadMetadata()
    {
        File curr_file = null;

        try
        {
            curr_file = new File(getRealPath());
            
            AudioFile audioFile = AudioFileIO.read(curr_file);

            if(audioFile == null)
                return;
            
            nombre_cancion = audioFile.getTag().getFirstTitle();
            nombre_artista = audioFile.getTag().getFirstArtist();
            nombre_disco = audioFile.getTag().getFirstAlbum();
            duracion = convertDuration(audioFile.getLength());
            
            /*System.out.println("duracion: "+audioFile.getLength());
            System.out.println("artista: "+audioFile.getTag().getFirstArtist());
            System.out.println("duracion guay: "+convertDuration(audioFile.getLength()));*/
        }
        catch (CannotReadException ex)
        {
            Logger.getLogger(Cancion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String convertDuration(int duration)
    {
        String aux = "";

        int hour;
        int min;
        int seg;

        hour = duration / 3600;
        min = duration / 60;
        seg = duration % 60;
        seg = (seg*60)/100;

        if(hour >= 1)
        {
            aux = hour+":"+min+":"+seg;
        }
        else
        {
            aux = min+":"+seg;
        }

        return new String(aux);
    }

    /*private void loadMetadata()
    {
        try
        {
            System.out.println("filename: "+getRealPath());
            MusicMetadataSet src_set = new MyID3().read(new File(getRealPath()));
            
            if(src_set == null)
                return;
            
            MusicMetadata metadata = (MusicMetadata) src_set.getSimplified();

            nombre_cancion = metadata.getSongTitle();
            nombre_artista = metadata.getArtist();
            nombre_disco = metadata.getAlbum();
            duracion = metadata.getDurationSeconds().intValue();
        }
        catch (ID3ReadException ex)
        {
            ex.printStackTrace();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }*/
}

